﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using ChiDao.Component.Utils;
using ChiDao.Business.Models;
using ChiDao.Business.Models.FlowProcessNew;
using ChiDao.Site.Models;
using ChiDao.Business.Data.Repositories.FlowProcessNew;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IHolidayRequestService))]
    internal class HolidayRequestService : IHolidayRequestService
    {
        [Import]
        protected IHolidayRequestRepository HolidayRequestRepository { get; set; }
        [Import]
        protected IProcessedTaskRepository ProcessedTaskRepository { get; set; }

        //获取某一特定的请假申请单
        public HolidayRequestView GetHolidayRequestByID(string id)
        {
            HolidayRequestView holidayRequestView = null;
            Guid queryID = Guid.Parse(id);
            IQueryable<HolidayRequest> holidayRequests = HolidayRequestRepository.Entities.Where(h => h.ID == queryID);
            if (holidayRequests.Count() > 0)
            {
                holidayRequestView = new HolidayRequestView();
                HolidayRequest holidayRequest = holidayRequests.First();
                holidayRequestView.ID = holidayRequest.ID;
                holidayRequestView.RequestPerson = holidayRequest.RequestPerson;
                holidayRequestView.RequestReason = holidayRequest.RequestReason;
                holidayRequestView.RequestStartDate = holidayRequest.RequestStartDate;
                holidayRequestView.RequestEndDate = holidayRequest.RequestEndDate;
                holidayRequestView.RequestDate = holidayRequest.RequestDate;
                holidayRequestView.HandleState = holidayRequest.HandleState;

                IQueryable<ProcessedTask> processedTasks = ProcessedTaskRepository.Entities.Where(b => b.ProcessID == id).OrderBy(b => b.ProcessTime);
                foreach (ProcessedTask processedTask in processedTasks)
                {
                    //IEnumerable<ProcessedTaskView> existsProcessedTaskViews = holidayRequestView.lstProcessedTaskView.Where(b => b.CurrentProcessNodeCode == processedTask.CurrentProcessNodeCode);
                    //if (existsProcessedTaskViews != null && existsProcessedTaskViews.Count() > 0)
                    //{
                    //    existsProcessedTaskViews.First().CurrentProcessPerson += "," + processedTask.CurrentProcessPerson;
                    //}
                    //else
                    //{
                    holidayRequestView.lstProcessedTaskView.Add(new ProcessedTaskView()
                    {
                        ID = processedTask.ID,
                        ProcessID = processedTask.ProcessID,
                        CurrentProcessNodeCode = processedTask.CurrentProcessNodeCode,
                        CurrentProcessNodeName = processedTask.CurrentProcessNodeName,
                        ApprovalOpinion = processedTask.ApprovalOpinion,
                        CurrentProcessPerson = processedTask.CurrentProcessPerson,
                        ProcessTime = processedTask.ProcessTime,
                        HandleState = processedTask.HandleState
                    });
                    //}
                }
            }

            return holidayRequestView;
        }

        //保存请假单据，存在则修改，否则新增
        public void SaveHoliday(Guid id, string userName, string requestReason, DateTime requestStartDate, DateTime requestEndDate)
        {
            HolidayRequest record = HolidayRequestRepository.Entities.FirstOrDefault(m => m.ID == id);
            if (record != null)
            {
                UpdateHoliday(id, requestReason, requestStartDate, requestEndDate, "0");
            }
            else
            {
                record = new HolidayRequest()
                {
                    ID = id,
                    RequestPerson = userName,
                    RequestReason = requestReason,
                    RequestStartDate = requestStartDate,
                    RequestEndDate = requestEndDate,
                    RequestDate = DateTime.Now,
                    HandleState = "0"
                };
                HolidayRequestRepository.Insert(record);
            }
        }

        //修改请假单据
        public void UpdateHoliday(Guid id, string requestReason, DateTime requestStartDate, DateTime requestEndDate, string handleState, bool isUpdateHandleState = false)
        {
            HolidayRequest record = HolidayRequestRepository.Entities.FirstOrDefault(m => m.ID == id);
            if (record != null)
            {
                if (!isUpdateHandleState)
                {
                    record.RequestReason = requestReason;
                    record.RequestStartDate = requestStartDate;
                    record.RequestEndDate = requestEndDate;
                    record.RequestDate = DateTime.Now;
                }
                record.HandleState = handleState;
                HolidayRequestRepository.Update(record);
            }
        }

        //获取某一用户的请假单列表
        public List<HolidayRequestView> GetHolidayRequestByRequestPerson(string userName)
        {
            List<HolidayRequestView> holidayRequestViewList = new List<HolidayRequestView>();
            IQueryable<HolidayRequest> holidayRequests = HolidayRequestRepository.Entities.Where(h => h.RequestPerson == userName);
            foreach (HolidayRequest holidayRequest in holidayRequests)
            {
                holidayRequestViewList.Add(new HolidayRequestView() { ID = holidayRequest.ID, RequestPerson = holidayRequest.RequestPerson, RequestReason = holidayRequest.RequestReason, RequestStartDate = holidayRequest.RequestStartDate, RequestEndDate = holidayRequest.RequestEndDate, RequestDate = holidayRequest.RequestDate, HandleState = holidayRequest.HandleState });
            }
            return holidayRequestViewList;
        }
    }
}